<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Orders
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Orders_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($uri, $num_rows = false)
	{
		$this->db->query("SET NAMES 'LATIN1'");
		if ($uri['limit'] == '') $uri['limit'] = 50;
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(`ma` LIKE "%%%s%%" OR `maxacthuc` LIKE "%%%s%%" OR `email_nguoimua` LIKE "%%%s%%" OR `dienthoai_nguoimua` LIKE "%%%s%%" OR `ten_nguoimua` LIKE "%%%s%%" OR `diachi_giaohang` LIKE "%%%s%%")', $uri['q'], $uri['q'], $uri['q'], $uri['q'], $uri['q'], $uri['q']));
		}
		if ($uri['from'] != '')
		{
			$this->db->where(sprintf('date_added >= %1$s', $this->db->escape($uri['from'].' 00:00:00')));
		}
		if ($uri['to'] != '')
		{
			$this->db->where(sprintf('date_added <= %1$s', $this->db->escape($uri['to'].' 23:59:59')));
		}
		if ($uri['status'] != '')
		{
			$this->db->where('status', $uri['status']);
		}
		$this->db->where('deleted', $uri['deleted']);
		if ($num_rows)
		{
			$this->db->select('id');
			return $this->db->get('orders')->num_rows();
		}
		else
		{
			$last_login = $GLOBALS['user']['last_login'];
			$this->db->select('*, IF ('.sprintf('date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
			$this->db->limit($uri['limit'], $uri['rowstart']);
			if ($uri['orderby'] == '') $uri['orderby'] = 'id';
			if ($uri['ordermode'] == '') $uri['ordermode'] = 'desc';
			$this->db->order_by($uri['orderby'].' '.$uri['ordermode']);
			$query = $this->db->get('orders');
			if ($query->num_rows() > 0)
			{
				$data = $query->result_array();
				$query->free_result();
				return $data;
			}
			else
			{
				return false;
			}
		}
	}

	public function filter_status_list() {
		$this->db->select('COUNT(id) AS product, status', false);
		$this->db->group_by('status');
		$this->db->where('deleted', 0);
		$query = $this->db->get('orders');
		$data = array();
		foreach ($query->result_array() as $val)
		{
			if ($val['product']) 
			{
				$data[] = array(
					'id' => $val['status'],
					'name_vn' => $GLOBALS['orders_status'][$val['status']],
					'parent' => 0,
					'chirld' => 0,
					'product' => $val['product']
				);
			}
		}
		return $data;
	}

	public function info($id)
	{
		$this->db->query("SET NAMES 'LATIN1'");
		$this->db->where('id', $id);
		$query = $this->db->get('orders');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('orders');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function product_list($ma)
	{
		$this->db->select('o.*, p.name_vn, p.img, p.code');
		$this->db->from('orders_details AS o');
		$this->db->join('products AS p', 'o.proid = p.id');
		$this->db->where('o.ma', $ma);
		$this->db->order_by('id desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) 
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function process($data, $id)
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query("SET NAMES 'LATIN1'");
		$ma = $data['ma'];
		unset($data['ma']);
		$this->db->where('id', $id);
		$this->db->update('orders', $data);
		write_log('update_orders', $ma);
		return true;
	}

}

/* End of file orders_model.php */
/* Location: ./application/models/orders_model.php */